package com.ruoyi.framework.security.filter;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.JacksonUtil;
import com.ruoyi.common.utils.sign.AES;
import com.ruoyi.framework.utils.SessionHelper;
import org.slf4j.Logger;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author xiaom
 * @Date 2020/12/23 16:17
 * @Version 1.0.0
 * @Description <>
 **/
public interface AbsFilter {

    Logger getLogger();

    default void responseException(HttpServletResponse response, Exception e,int status) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=UTF-8");
        try {
            if(SessionHelper.isClientRequest()){
                response.getWriter().write(AES.encryptAES(JacksonUtil.toJson(AjaxResult.error(status, e.getMessage()))));
            }else{
                response.getWriter().write(JacksonUtil.toJson(AjaxResult.error(status, e.getMessage())));
            }
        } catch (Exception ex) {
            getLogger().error("加密文本失败--> {}",e.getMessage());
        }
        response.getWriter().close();
    }

    default void responseSuccess(HttpServletResponse response, Object date) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=UTF-8");
        try {
            response.getWriter().write(AES.encryptAES(JSON.toJSONString(AjaxResult.success(date))));
        } catch (Exception ex) {
        }
        response.getWriter().close();
    }

}
